package com.newsee.remote.budget.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newsee.remote.common.database.entity.ShSapbudgetinfo;
import com.newsee.remote.common.database.entity.ShSapcostcenter;
import com.newsee.remote.common.database.mapper.ShSapbudgetinfoMapper;
import com.newsee.remote.budget.service.IShSapbudgetinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newsee.remote.common.dto.budget.*;
import com.newsee.remote.common.dto.costcenter.CostCenterDTO;
import com.newsee.remote.common.dto.costcenter.CostHeadDTO;
import com.newsee.remote.common.dto.costcenter.CostIRequestDTO;
import com.newsee.remote.common.dto.costcenter.CostReqBaseInfoDTO;
import com.newsee.remote.common.enumeration.SrmRspStatusCodeEnum;
import com.newsee.remote.common.utils.tools.DateUtils;
import com.newsee.remote.common.vo.budget.BudgetVO;
import com.newsee.remote.common.vo.costcenter.CostCenterVO;
import com.newsee.remote.common.vo.supplier.EResponseVO;
import com.newsee.remote.common.vo.supplier.MessageVO;
import com.newsee.remote.common.vo.supplier.RspBaseInfoVO;
import com.newsee.remote.common.vo.supplier.RspItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangzhichen
 * @since 2019-11-06
 */
@Slf4j
@Service
public class ShSapbudgetinfoServiceImpl extends ServiceImpl<ShSapbudgetinfoMapper, ShSapbudgetinfo> implements IShSapbudgetinfoService {

    @Override
    public BudgetVO acceptSapBudget(BudgetDTO budgetDTO) {
        BudgetVO vo = new BudgetVO();

        if (!checkData(budgetDTO)) {
            generatorRspBaseInfo(budgetDTO, vo, SrmRspStatusCodeEnum.SEND_MESSAGE_ERROR.getCode());
            return vo;
        }

        try {
            //srm提供的供应商报文
            List<BudReqItemDTO> heads = budgetDTO.getI_REQUEST().getMESSAGE().getREQ_ITEM();

            for (BudReqItemDTO head : heads) {
                //唯一标识
                String budgetItemCode = head.getBG_SUBJ();
                ShSapbudgetinfo budget = getOne(new QueryWrapper<ShSapbudgetinfo>().eq("budgetItemCode", budgetItemCode));

                //新增
                if (null == budget){
                    budget = new ShSapbudgetinfo();
                    budget.setCreateTime(LocalDateTime.now());
                }else {
                    budget.setUpdateTime(LocalDateTime.now());
                }
                budget.setBudgetItemCode(head.getBG_SUBJ());
                budget.setBudgetItemName(head.getBG_SUBJ_TX());
                budget.setParentCode(head.getSJ_SUBJ());
                budget.setIsLastBudgetItem(head.getZLEAF());
                //在用
                if (null != head.getIS_FLAG() && "X".equals(head.getIS_FLAG())) {
                    budget.setIsFlag(0);
                } else {
                    budget.setIsFlag(1);
                }
                saveOrUpdate(budget);
            }

            generatorRspBaseInfo(budgetDTO, vo, SrmRspStatusCodeEnum.HANDLE_SUCCESS.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("组织架构同步出错：" + e.getMessage());
            generatorRspBaseInfo(budgetDTO, vo, SrmRspStatusCodeEnum.INTERNAL_ERROR.getCode());
            return vo;
        }
        return vo;
    }

    private boolean checkData(BudgetDTO centerDTO) {
        //默认成功
        Boolean flag = true;
        return true;
    }


    /**
     * 生成响应报文基础信息名称
     *
     * @param dto 请求
     * @param vo  返回
     */
    private void generatorRspBaseInfo(BudgetDTO dto, BudgetVO vo, String rspStatsCode) {
        //请求报文根节点
        BudIRequestDTO iRequest = dto.getI_REQUEST();
        //请求报文基本信息
        BudReqBaseInfoDTO reqBaseinfo = iRequest.getREQ_BASEINFO();


        //-------封装返回对象--------
        //响应报文根节点名称
        EResponseVO eResponseVO = new EResponseVO();
        vo.setE_RESPONSE(eResponseVO);

        //响应报文基础信息名称
        RspBaseInfoVO rspBaseInfoVO = new RspBaseInfoVO();
        eResponseVO.setRSP_BASEINFO(rspBaseInfoVO);
        //接收系统返回消息，产生的 TRACE_ID，消息的唯一标识
        rspBaseInfoVO.setRSP_TRACE_ID(UUID.randomUUID().toString().replace("-", ""));
        //响应请求 TRACE_ID
        rspBaseInfoVO.setRSP_REQ_TRACEID(reqBaseinfo.getREQ_TRACE_ID());
        //报文发送时间，14位数字
        rspBaseInfoVO.setRSP_SEND_TIME(Long.valueOf(DateUtils.format(new Date(), DateUtils.DATE_PATTERN_FULL)));
        //发送方系统标识
        rspBaseInfoVO.setRSP_SRC_SYS(reqBaseinfo.getREQ_SRC_SYS());
        //接收方系统标识:
        rspBaseInfoVO.setRSP_TAR_SYS("BS-WY-RFWZ");
        //PO 或 ESB 上定义的SI名称，由PO或ESB/顾问提供
        rspBaseInfoVO.setRSP_SERVER_NAME(reqBaseinfo.getREQ_SERVER_NAME());
        //业务系统流水号或业务唯一编号，从请求报文中获取
        rspBaseInfoVO.setRSP_BSN_ID(reqBaseinfo.getREQ_BSN_ID());
        //接口处理状态代码
        rspBaseInfoVO.setRSP_STATUS_CODE(rspStatsCode);

        //给srm返回的信息
        MessageVO messageVO = new MessageVO();
        RspItemVO rspItemVO = new RspItemVO();
        eResponseVO.setMESSAGE(messageVO);
        messageVO.setRSP_ITEM(rspItemVO);
        rspItemVO.setRSP_STATUS_CODE(rspStatsCode);
        rspItemVO.setRSP_STATUS_MSG(SrmRspStatusCodeEnum.getTextByCode(rspStatsCode));
    }

}
